import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * 扑克牌类，可以被翻开
 */
public class Card extends Actor {
    private int value = -1;
    private boolean isFaceUp = false;
    private GreenfootImage faceUpImage = null;
    private GreenfootImage faceDownImage = null;

    private int speed;
    private int direction;
    private int moveCounter = 0; // 移动计数器

    public Card(int cardValue) {
        value = cardValue;
        isFaceUp = false;
        String fileName = "hearts" + value + ".png";
        faceUpImage = new GreenfootImage(fileName.toLowerCase());
        faceDownImage = new GreenfootImage("blueflip.png");
        setImage(faceDownImage);

        speed = Greenfoot.getRandomNumber(5) + 1; // 随机速度 
        direction = Greenfoot.getRandomNumber(360);
    }

    public void act() {
        moveCounter++; // 增加计数器

        // 每60帧移动一次
        if (moveCounter % 60 == 0) {
            moveRandom();
        }

        if (Greenfoot.mouseClicked(this)) {
            if (!isFaceUp) {
                setImage(faceUpImage);
                isFaceUp = true;
                ((Table) getWorld()).incrementScore();
            }
        }
    }

    public void moveRandom() {
        int angle = Greenfoot.getRandomNumber(360);
        int distance = Greenfoot.getRandomNumber(10) + 1; // 控制移动距离
        setRotation(angle);
        move(distance);
    }

    public int getValue() {
        return value;
    }

    public boolean getFaceup() {
        return isFaceUp;
    }

    public void turnFaceDown() {
        isFaceUp = false;
        setImage(faceDownImage);
    }
}

